import Vue from 'vue'
import Vuex from 'vuex'
import pre from 'vuex-persistedstate'
import { getLogin } from '../utils/api'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: ''
  },
  getters: {

  },
  mutations: {
    setLogin(state, token) {
      state.token = token
      localStorage.setItem('token', token)
    }
  },
  actions: {
    setLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        getLogin(userInfo).then(res => {
          if (res.meta.status === 200) {
            commit('setLogin', res.data.token)
            resolve(res)
          }
        })
      })
    }
  },
  modules: {
  },
  plugins: [pre()]
})
